<?php
use Out;

abstract class Data
{
    public static $db    = null;
    public static $dbs   = [];
    public static $error = false;

    //获取数据连接
    public static function Db()
    {
        extract(Sys::$conf['DB']);
        isset($db_error) && self::$error = $db_error;
        isset($db_port) && !empty($db_port) && $db_host .= ":" . $db_port;
        $dsn = "{$db_type}:host={$db_host};dbname={$db_name}";
        try {
            self::$db = new PDO($dsn, $db_user, $db_pass, [PDO::ATTR_PERSISTENT => true]);
        } catch (\Exception $e) {
            throw new \Exception($e);
        }
        self::$db->exec("set names 'utf8';");
    }

    public static function Filter(&$data, $rules)
    {
        if (!isset($data) || count($data) < 1) {
            return $data;
        }
        return Data\Filter::Run($data, $rules);
    }

    public static function Error($sql, $file, $line)
    {
        self::$error && Sys::Error("x", "SQL异常 {$sql}", $file, $line);
        Out::Error("SQL异常:{$sql}");
    }

    public static function __callStatic($method, $param)
    {
        empty(self::$db) && self::Db();
        $class = "\\Data\\{$method}";
        return $class::Run($param[0]);
    }

}
